﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModelLibrary;

namespace DataBaseBackupTools
{
    public partial class FmMain : Form
    {
        public FmMain()
        {
            InitializeComponent();
            this.splitContainer1.Panel1.Paint += new PaintEventHandler(MenuBtn_Paint);
            this.splitContainer1.Panel1.Paint += new PaintEventHandler(CloseCtr_Paint);
            this.splitContainer1.Panel1.MouseClick += new MouseEventHandler(MenuBtn_OnClick);
            this.splitContainer1.Panel1.MouseClick += new MouseEventHandler(CloseCtr_OnClick);
            this.splitContainer1.Panel1.MouseMove += new MouseEventHandler(MenuBtn_MouseMove);
            this.splitContainer1.Panel1.MouseMove += new MouseEventHandler(CloseCtr_MouseMove);
            this.SizeChanged += new EventHandler(Form_SizeChange);
        }
        //缩放按钮颜色是否变更过
        private static bool IsColorChange = false;
        //缩放按钮尖的方向
        private char LR = 'L';
        //当前选择菜单索引
        private int selectIndex = 0;
        //当前移动到菜单的索引
        private int mouseMoveOnIndex = -1;
        //菜单是否初始化刷新过
        private static bool Initialize = false;
        private void FmMain_Load(object sender, EventArgs e)
        {
            状态信息_Click(sender, new EventArgs());
            string Mail = @"Mail : renr12@163.com";
            this.toolStripStatusLabel1.Text = Mail;
        }
        #region 菜单功能
        private void 状态信息_Click(object sender, EventArgs e)
        {
            HidenForms();
            Control FmStatus;
            if (!CanGetForms("FmStatus", out FmStatus))
            {
                FmStatus = new FmStatus();
                FmStatus.Dock = DockStyle.Fill;
                FmStatus.Show();
                this.splitContainer1.Panel2.Controls.Add(FmStatus);
            }

        }

        private void 实例信息_Click(object sender, EventArgs e)
        {
            HidenForms();
            Control FmInstance;
            if (!CanGetForms("FmInstance", out FmInstance))
            {
                FmInstance = new FmInstance();
                FmInstance.Dock = DockStyle.Fill;
                FmInstance.Show();
                this.splitContainer1.Panel2.Controls.Add(FmInstance);
            }

        }

        private void 任务信息_Click(object sender, EventArgs e)
        {
            HidenForms();
            Control FmTask;
            if (!CanGetForms("FmTask", out FmTask))
            {
                FmTask = new FmTask();
                FmTask.Dock = DockStyle.Fill;
                FmTask.Show();
                this.splitContainer1.Panel2.Controls.Add(FmTask);
            }
        }

        private void 日志信息_Click(object sender, EventArgs e)
        {
            HidenForms();
            Control FmLogs;
            if (!CanGetForms("FmLogs", out FmLogs))
            {
                FmLogs = new FmLogs();
                FmLogs.Dock = DockStyle.Fill;
                FmLogs.Show();
                this.splitContainer1.Panel2.Controls.Add(FmLogs);
            }
        }

        private void 其他设置_Click(object sender, EventArgs e)
        {
            HidenForms();
            Control FmSets;
            if (!CanGetForms("FmSets", out FmSets))
            {
                FmSets = new FmSets();
                FmSets.Dock = DockStyle.Fill;
                FmSets.Show();
                this.splitContainer1.Panel2.Controls.Add(FmSets);
            }
        }

        private void HidenForms()
        {
            foreach (Control item in this.splitContainer1.Panel2.Controls)
            {
                item.Visible = false;
                item.Enabled = false;
            }
        }

        private bool CanGetForms(string formName, out Control Fm)
        {
            foreach (Control item in this.splitContainer1.Panel2.Controls)
            {
                if (item.Name == formName)
                {
                    item.Visible = true;
                    item.Enabled = true;
                    Fm = item;
                    return true;
                }
            }
            Fm = null;
            return false;
        }

        #endregion
        #region 绘制缩放按钮

        private void CloseCtr_Paint(object sender, PaintEventArgs e)//绘制缩放按钮
        {
            Size size = new Size(30, 30);
            new DrawingZoomBtn(this.splitContainer1.Panel1, size, Brushes.LightGray, Brushes.Gray, LR = this.splitContainer1.SplitterDistance == 26 ? 'R' : 'L');
        }
        private void CloseCtr_OnClick(object sender, MouseEventArgs e)//点击缩放按钮的动作
        {
            if (e.X > ((Control)sender).Width - 26 && e.X < ((Control)sender).Width - 2 && e.Y > 2 && e.Y < 26 && e.Button == MouseButtons.Left)
            {
                this.splitContainer1.SplitterDistance = this.splitContainer1.SplitterDistance == 26 ? 176 : 26;
            }
        }
        private void CloseCtr_MouseMove(object sender, MouseEventArgs e)//在Panel1移动鼠标时缩放按钮的变化
        {
            Size size = new Size(30, 30);
            if (e.X > ((Control)sender).Width - 26 && e.X < ((Control)sender).Width - 2 && e.Y > 2 && e.Y < 26)
            {
                if (IsColorChange)
                {
                    new DrawingZoomBtn(this.splitContainer1.Panel1, size, Brushes.Gray, Brushes.DarkGray, LR = this.splitContainer1.SplitterDistance == 26 ? 'R' : 'L');
                    IsColorChange = false;
                }
            }
            else
            {
                if (!IsColorChange)
                {
                    IsColorChange = true;
                    new DrawingZoomBtn(this.splitContainer1.Panel1, size, Brushes.LightGray, Brushes.Gray, LR = this.splitContainer1.SplitterDistance == 26 ? 'R' : 'L');
                }
            }
        }
        private void Form_SizeChange(object sender, EventArgs e)//窗口大小发生变化时，重绘左边Panel1
        {
            this.splitContainer1.SplitterDistance = LR == 'R' ? 26 : 176;
        }
        #endregion
        #region 绘制菜单按钮

        private void MenuBtn_Paint(object sender, PaintEventArgs e)//绘制菜单按钮
        {
            if (Initialize)
            {
                return;
            }
            SplitterPanel sp = (SplitterPanel)sender;
            for (int i = 0; i <= 5; i++)
            {
                if (i == selectIndex)
                    MenuBtn_Paint(sp, i, Brushes.DarkGray, Brushes.Black);
                else
                    MenuBtn_Paint(sp, i, Brushes.LightGray, Brushes.Gray);
            }
            Initialize = true;
        }
        private void MenuBtn_OnClick(object sender, MouseEventArgs e)//点击菜单按钮的动作
        {
            Initialize = false;
            SplitterPanel sp = (SplitterPanel)sender;
            if (e.Button == MouseButtons.Left)
            {
                if (e.Y > 0 && e.Y < 61 * 1)
                {
                    if (!(e.X > sp.Width - 26 && e.X < sp.Width - 2 && e.Y > 2 && e.Y < 26))
                    {
                        状态信息_Click(sender, new EventArgs());
                        if (selectIndex != -1)
                        {
                            MenuBtn_Paint(sp, selectIndex, Brushes.LightGray, Brushes.Gray);
                        }
                        selectIndex = 0;
                        MenuBtn_Paint(sp, selectIndex, Brushes.DarkGray, Brushes.Black);
                    }
                }
                else if (e.Y > 61 * 1 && e.Y < 61 * 2)
                {
                    实例信息_Click(sp, new EventArgs());
                    if (selectIndex != 1)
                    {
                        MenuBtn_Paint(sp, selectIndex, Brushes.LightGray, Brushes.Gray);
                        selectIndex = 1;
                        MenuBtn_Paint(sp, selectIndex, Brushes.DarkGray, Brushes.Black);
                    }
                }
                else if (e.Y > 61 * 2 && e.Y < 61 * 3)
                {
                    任务信息_Click(sp, new EventArgs());
                    if (selectIndex != 2)
                    {
                        MenuBtn_Paint(sp, selectIndex, Brushes.LightGray, Brushes.Gray);
                        selectIndex = 2;
                        MenuBtn_Paint(sp, selectIndex, Brushes.DarkGray, Brushes.Black);
                    }
                }
                else if (e.Y > 61 * 3 && e.Y < 61 * 4)
                {
                    日志信息_Click(sp, new EventArgs());
                    if (selectIndex != 3)
                    {
                        MenuBtn_Paint(sp, selectIndex, Brushes.LightGray, Brushes.Gray);
                        selectIndex = 3;
                        MenuBtn_Paint(sp, selectIndex, Brushes.DarkGray, Brushes.Black);
                    }
                }
                else if (e.Y > 61 * 4 && e.Y < 61 * 5)
                {
                    其他设置_Click(sp, new EventArgs());
                    if (selectIndex != 4)
                    {
                        MenuBtn_Paint(sp, selectIndex, Brushes.LightGray, Brushes.Gray);
                        selectIndex = 4;
                        MenuBtn_Paint(sp, selectIndex, Brushes.DarkGray, Brushes.Black);
                    }
                }


            }
        }
        private void MenuBtn_MouseMove(object sender, MouseEventArgs e)//在Panel1移动鼠
        {
            SplitterPanel sp = (SplitterPanel)sender;
            if (e.Y > 0 && e.Y < 61 * 1)
            {
                if (!(e.X > ((Control)sender).Width - 26 && e.X < ((Control)sender).Width - 2 && e.Y > 2 && e.Y < 26))
                {
                    if (mouseMoveOnIndex != 0)
                    {
                        if (mouseMoveOnIndex != -1)
                        {
                            if (selectIndex != mouseMoveOnIndex)
                                MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.LightGray, Brushes.Gray);
                        }
                        mouseMoveOnIndex = 0;
                        if (selectIndex != mouseMoveOnIndex)
                            MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.Gray, Brushes.DarkGray);
                    }
                }
            }
            else if (e.Y > 61 * 1 && e.Y < 61 * 2)
            {
                if (mouseMoveOnIndex != 1)
                {
                    if (mouseMoveOnIndex != -1)
                    {
                        if (selectIndex != mouseMoveOnIndex)
                            MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.LightGray, Brushes.Gray);
                    }
                    mouseMoveOnIndex = 1;
                    if (selectIndex != mouseMoveOnIndex)
                        MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.Gray, Brushes.DarkGray);
                }
            }
            else if (e.Y > 61 * 2 && e.Y < 61 * 3)
            {
                if (mouseMoveOnIndex != 2)
                {
                    if (mouseMoveOnIndex != -1)
                    {
                        if (selectIndex != mouseMoveOnIndex)
                            MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.LightGray, Brushes.Gray);
                    }
                    mouseMoveOnIndex = 2;
                    if (selectIndex != mouseMoveOnIndex)
                        MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.Gray, Brushes.DarkGray);
                }
            }
            else if (e.Y > 61 * 3 && e.Y < 61 * 4)
            {
                if (mouseMoveOnIndex != 3)
                {
                    if (mouseMoveOnIndex != -1)
                    {
                        if (selectIndex != mouseMoveOnIndex)
                            MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.LightGray, Brushes.Gray);
                    }
                    mouseMoveOnIndex = 3;
                    if (selectIndex != mouseMoveOnIndex)
                        MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.Gray, Brushes.DarkGray);
                }
            }
            else if (e.Y > 61 * 4 && e.Y < 61 * 5)
            {
                if (mouseMoveOnIndex != 4)
                {
                    if (mouseMoveOnIndex != -1)
                    {
                        if (selectIndex != mouseMoveOnIndex)
                            MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.LightGray, Brushes.Gray);
                    }
                    mouseMoveOnIndex = 4;
                    if (selectIndex != mouseMoveOnIndex)
                        MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.Gray, Brushes.DarkGray);
                }
            }
            else
            {
                if (mouseMoveOnIndex != -1)
                {
                    MenuBtn_Paint(sp, mouseMoveOnIndex, Brushes.LightGray, Brushes.Gray);
                    mouseMoveOnIndex = -1;
                    MenuBtn_Paint(sp, selectIndex, Brushes.DarkGray, Brushes.Black);
                }
            }
        }
        private void MenuBtn_Paint(SplitterPanel sp, int index, Brush brushBg, Brush brushFt)//绘制菜单按钮
        {
            Point pt;
            Font ft = new Font("华文彩云", 22f);
            switch (index)
            {
                case 0:
                    //状态信息
                    pt = new Point(0, 61 * index);
                    new DrawingMainMenuBtn(sp, new Size(sp.Width, 60), brushBg, brushFt, ft, "状态信息", pt);
                    break;
                case 1:
                    //实例信息
                    pt = new Point(0, 61 * index);
                    new DrawingMainMenuBtn(sp, new Size(sp.Width, 60), brushBg, brushFt, ft, "实例信息", pt);
                    break;
                case 2:
                    //任务信息
                    pt = new Point(0, 61 * index);
                    new DrawingMainMenuBtn(sp, new Size(sp.Width, 60), brushBg, brushFt, ft, "任务信息", pt);
                    break;
                case 3:
                    //日志信息
                    pt = new Point(0, 61 * index);
                    new DrawingMainMenuBtn(sp, new Size(sp.Width, 60), brushBg, brushFt, ft, "日志信息", pt);
                    break;
                case 4:
                    //其他设置
                    pt = new Point(0, 61 * index);
                    new DrawingMainMenuBtn(sp, new Size(sp.Width, 60), brushBg, brushFt, ft, "其他设置", pt);
                    break;
            }
            CloseCtr_Paint(sp, null);
        }

        #endregion
    }

}
